package ThreadCurr;

public class BankTest {
    static boolean success1 = false;
    static boolean success2 = false;

    public static  void main(String[] args) {
        /*Bank bank = new Bank();
        Boolean result = bank.getMoney(200000);
        System.out.println("是否取钱成功"+result+"余额还有"+bank.getAccount());*/
        Bank bank = new Bank();
        int num=1;
        while (true) {
            Thread t1 = new Thread("李浩博") {
                public void run() {
                    success1 = bank.getMoney(20000);
                }
            };
            Thread t2 = new Thread("袁瑞浩") {
                public void run() {
                    success2 = bank.getMoney(20000);
                }
            };
            t1.start();
            t2.start();

            //让主线程阻塞，目的是等待t1,t2执行完毕
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            if (success1 && success2) {
                System.out.println("全部取款成功，出现了并发安全问题!");
                System.out.println("共测试了:"+num+"次");
                break;
            }else {
                num++;
                success1=false;
                success2=false;
                bank.saveAccount(20000);
            }

        }
    }
}
